package Servlets.yz;

import Bean.supplier;
import Util.GokJDBCUtil2;
import com.mysql.cj.util.StringUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
* 供应商的展示功能
* */
@WebServlet(name = "SupplierSServlet",urlPatterns = "/SupplierSServlet")
public class SupplierSServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        String method = req.getParameter("method");//获取查询方法
        String contractname = req.getParameter("contractname");//获取供应商人的名字
        String phone = req.getParameter("phone");//获取供应商的电话
        List<supplier> suppliers = null;//存储查询到的元素

        /*分页*/
        int page=1;
        int pageSize=4;
        //计算总记录数量
        String countSql = "select count(*) from supplier where isdeleted=0";
        int totalcount = GokJDBCUtil2.queryForCount(countSql);
        int totalpages = ((totalcount + pageSize - 1) / pageSize);

        String currentpage = req.getParameter("page");
        //在前端获取到的page为空时默认为1
        if(req.getParameter("page")==null){
            page=1;
        }
        //page不为空的情况，将获取到的page数据赋值到后端的page
        else {
            page=Integer.parseInt(currentpage);
            if(page > totalpages){
                page=totalpages;
            }
        }
        //设置开始索引
        int startIndex = (page-1)*pageSize;
        /*
        * 用于实现1-2种的信息模糊查询
        * */
        if("select".equals(method)){
            //检测从最上面获取到的contractname和phone是否都不为空
            if(!StringUtils.isNullOrEmpty(contractname) && !StringUtils.isNullOrEmpty(phone)){
                String sql = "select * from supplier where contactperson like ? and phonenumber like ? limit ?,?";
                suppliers = GokJDBCUtil2.queryAll(sql, supplier.class, "%" + contractname + "%", "%" + phone + "%",startIndex,pageSize);
                req.setAttribute("suppliers",suppliers);
                req.setAttribute("contractname",contractname);
                req.setAttribute("phone",phone);
            }
            //检测从最上面获取到的contractname是否不为空
            else if(!StringUtils.isNullOrEmpty(contractname)){
                String sql = "select * from supplier where contactperson like ? limit ?,?";
                suppliers = GokJDBCUtil2.queryAll(sql, supplier.class, "%" + contractname + "%",startIndex,pageSize);
                req.setAttribute("suppliers",suppliers);
                req.setAttribute("contractname",contractname);
            }
            //检测从最上面获取到的phone是否不为空
            else {
                String sql = "select * from supplier where phonenumber like ? limit ?,?";
                suppliers = GokJDBCUtil2.queryAll(sql, supplier.class, "%" + phone + "%",startIndex,pageSize);
                req.setAttribute("suppliers", suppliers);
                req.setAttribute("phone",phone);
            }
        }
        /*
        * 不进行查询功能的情况直接展示全部信息（分页）
        * */
        else {
            String sql = "select * from supplier  where isdeleted=0 limit ?,?";
            suppliers = GokJDBCUtil2.queryAll(sql, supplier.class,startIndex,pageSize);//获取供应商全部信息
            req.setAttribute("suppliers",suppliers);//该数据用于发送供应商的全部信息
        }
        req.setAttribute("totalcount",totalcount);//传输总记录数
        req.setAttribute("totalpages",totalpages);//传输总页数
        req.setAttribute("page",page);//传输当前页数
        req.getRequestDispatcher("pages/purmanger.jsp").forward(req,resp);//转到供应商展示界面
        writer.close();
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
